/*
 * @Description:
 * @Autor: fage
 * @Date: 2022-06-06 14:49:49
 * @LastEditors: lanmeng656 cbf0311@sina.com
 * @LastEditTime: 2022-11-17 16:46:16
 */
"use strict";
const db = require("../utils/db");
let errorTimes = 0;
module.exports = function (req, res, next) {
  let { username, password } = req.body;
  const admin = db.get("admin");
  if (username == admin.username && admin.password == password) {
    req.session.admin = username;
    return res.json({ msg: "ok" });
  } else {
    errorTimes++;
    if (errorTimes > 4) {
      if (errorTimes == 8) {
        setTimeout((t) => {
          errorTimes = 0;
        }, 3600000 * 5);
        return res.json({ msg: "登录错误次数太多，系统已锁定,5小时后解锁." });
      }
      return res.json({ msg: "登录错误次数太多，系统已锁定" });
    }
  }
  res.json({ msg: "用户名或密码错误" });
};
